*     *****************************************************************   *;
*     *****************************************************************   *;
*   File-Name:  undisasteraid_final.do   	      			              *;
*   Date:       Dec 30, 2020                                       	      *;
*   Authors:    Dellmuth, Rosvold, Jönsson, Bender, and von Uexkull       *;
*   Purpose:    Replicate regression and prediction results in paper      *;
*   "Humanitarian Need Drives Multilateral Disaster Aid", Dellmuth et al. *;
*   2021 in PNAS													      *;
*   Input File: undisasteraid_final_forrep.dta                            *; 
*     ****************************************************************    *;
*     ****************************************************************    *;


* Content: Do-file for out of sample predictions to cross validate the regression results.
* For the remainder of the code, see undisasteraid_final.do.
 
*Note: undisasteraid_final_forrep.dta is the final dataset including additional identfiers for dividing the sample in 5 folds, which preserve country year clusters, and repeat this 10 times, each time randomly changing the draws.
* it also specifies the main sample all models are evaluated on.  The do file runs out-of-sample evaluations for all dependent variables separately and first compares needs, strategic and baseline and thereafter needs, strategic and the full models.
   

/*
forvalues seed = 1(1)10 {
bysort cnum year: gen randnum`seed' = rnormal() 
bysort cnum year: replace randnum`seed' = randnum`seed'[1]
egen fold`seed' = cut(randnum`seed'), group(5)
} 
 

logit cerfd mag1 ltotalaffectedemdata1000  totalsfi  ptassigned  p5colony ip_lnacorcount resid_emergoda agree  conflict drought extmix flood storm, cluster(country)
 gen samplecv=e(sample) 

save "undisasteraid_final_forrep.dta" */

vers 15.1 // Stata SE

use "undisasteraid_final_forrep.dta"
 

*1) CERF 
*1.1) CERF needs vs strategic vs baseline 


set graphics off

set seed 586358

forvalues seed = 1(1)10 {
    display "Now doing iteration: `seed'/10"
    forvalues current_fold = 0(1)4 {
        display "k-fold is: `current_fold' (out of 5-folds)"

        display "Model I needs: "
        qui logit cerfd mag1 ltotalaffectedemdata1000  totalsfi conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country)
        predict fcastI`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model II strategic : " 
		qui logit cerfd ptassigned  p5colony ip_lnacorcount resid_emergoda agree conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model III baseline: "
        qui logit cerfd  conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastIII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
        eststo model`current_fold'`seed'
		
		
        roctab cerfd fcastI`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucI`current_fold'`seed'=r(area)
    
        
        roctab cerfd  fcastII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucII`current_fold'`seed'=r(area)
     

        roctab cerfd  fcastIII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucIII`current_fold'`seed'=r(area)
    

        local vname fcastI`current_fold'`seed' fcastII`current_fold'`seed' fcastIII`current_fold'`seed'
        local vlist `vlist' cerfd `vname'

    }
}

set graphics on 
display "`vlist'"

gen roccumI = 0
gen roccumII = 0
gen roccumIII = 0


forvalues i=0(1)4 {
     forvalues j=1(1)10 {
           replace roccumI = roccumI + rocaucI`i'`j'
           replace roccumII = roccumII + rocaucII`i'`j'
           replace roccumIII = roccumIII + rocaucIII`i'`j'
          
     }
}

replace roccumI = roccumI/50
replace roccumII = roccumII/50
replace roccumIII = roccumIII/50


estpost summarize rocauc*


quietly sum roccumI
display "Cummulative ROC model I needs: `r(mean)'"
quietly sum roccumII
display "Cummulative ROC model II strategic: `r(mean)'"
quietly sum roccumIII
display "Cummulative ROC model III baseline: `r(mean)'"



/*

. quietly sum roccumI

. display "Cummulative ROC model I needs: `r(mean)'"
Cummulative ROC model I needs: .7586877346038818

. quietly sum roccumII

. display "Cummulative ROC model II strategic: `r(mean)'"
Cummulative ROC model II strategic: .7343909740447998

. quietly sum roccumIII

. display "Cummulative ROC model III baseline: `r(mean)'"
Cummulative ROC model III baseline: .6911133527755737


 */


pause 




*1.2) CERF needs vs strategic vs full



set graphics off 
display "`vlist'"
 
drop fcast*
drop roc* 
 
  

set seed 586358

forvalues seed = 1(1)10 {
    display "Now doing iteration: `seed'/10"
    forvalues current_fold = 0(1)4 {
        display "k-fold is: `current_fold' (out of 5-folds)"

        display "Model I needs: "
        qui logit cerfd mag1 ltotalaffectedemdata1000 totalsfi  conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country)
        predict fcastI`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model II strategic : " 
		qui logit cerfd ptassigned  p5colony ip_lnacorcount resid_emergoda agree  conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model IV full: "
        qui logit cerfd mag1 ltotalaffectedemdata1000  conflict  totalsfi ptassigned  p5colony ip_lnacorcount resid_emergoda agree drought extmix flood storm if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastIII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
        eststo model`current_fold'`seed'

		
        roctab cerfd fcastI`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucI`current_fold'`seed'=r(area)
      
        
        roctab cerfd  fcastII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucII`current_fold'`seed'=r(area)
     

        roctab cerfd  fcastIII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucIII`current_fold'`seed'=r(area)
  

        local vname fcastI`current_fold'`seed' fcastII`current_fold'`seed' fcastIII`current_fold'`seed'
        local vlist `vlist' cerfd `vname'

    }
}

set graphics on 
display "`vlist'"

gen roccumI = 0
gen roccumII = 0
gen roccumIII = 0


forvalues i=0(1)4 {
     forvalues j=1(1)10 {
           replace roccumI = roccumI + rocaucI`i'`j'
           replace roccumII = roccumII + rocaucII`i'`j'
           replace roccumIII = roccumIII + rocaucIII`i'`j'
        
     }
}

replace roccumI = roccumI/50
replace roccumII = roccumII/50
replace roccumIII = roccumIII/50


estpost summarize rocauc*


quietly sum roccumI
display "Cummulative ROC model I nneds: `r(mean)'"
quietly sum roccumII
display "Cummulative ROC model II strategic: `r(mean)'"
quietly sum roccumIII
display "Cummulative ROC model IV full: `r(mean)'"

 
 /*
. display "Cummulative ROC model I needs: `r(mean)'"
Cummulative ROC model I: .7586877346038818

. quietly sum roccumII

. display "Cummulative ROC model II strategic: `r(mean)'"
Cummulative ROC model II: .7343909740447998

. quietly sum roccumIII

. display "Cummulative ROC model IV full: `r(mean)'"
Cummulative ROC model IV: .7727059125900269 */

pause 







* 2) CBPF
* 2.1) CBPF needs vs strategic vs baseline 




local vlistXX

set graphics off
 
 



drop fcast*
drop roc* 
  

set seed 586358

forvalues seed = 1(1)10 {
    display "Now doing iteration: `seed'/10"
    forvalues current_fold = 0(1)4 {
        display "k-fold is: `current_fold' (out of 5-folds)"

        display "Model I needs: "
        qui logit cbpfd mag1 ltotalaffectedemdata1000  totalsfi  conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country)
        predict fcastI`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model II strategic: " //we can take out agree here and rerun it which is easier as looping again over everything
		qui logit cbpfd ptassigned  p5colony ip_lnacorcount resid_emergoda  conflict agree drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model III baseline: "
        qui logit cbpfd  conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastIII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
        eststo model`current_fold'`seed'
		
        roctab cbpfd fcastI`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucI`current_fold'`seed'=r(area)
     
        
        roctab cbpfd  fcastII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucII`current_fold'`seed'=r(area)
     

        roctab cbpfd  fcastIII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucIII`current_fold'`seed'=r(area)
     

        local vname fcastI`current_fold'`seed' fcastII`current_fold'`seed' fcastIII`current_fold'`seed'
        local vlist `vlist' cbpfd `vname'

    }
}

set graphics on 
display "`vlist'"

gen roccumI = 0
gen roccumII = 0
gen roccumIII = 0


forvalues i=0(1)4 {
     forvalues j=1(1)10 {
           replace roccumI = roccumI + rocaucI`i'`j'
           replace roccumII = roccumII + rocaucII`i'`j'
           replace roccumIII = roccumIII + rocaucIII`i'`j'
         
     }
}

replace roccumI = roccumI/50
replace roccumII = roccumII/50
replace roccumIII = roccumIII/50


estpost summarize rocauc*

quietly sum roccumI
display "Cummulative ROC model I needs: `r(mean)'"
quietly sum roccumII
display "Cummulative ROC model II strategic: `r(mean)'"
quietly sum roccumIII
display "Cummulative ROC model III baseline: `r(mean)'"


/* . display "Cummulative ROC model I needs: `r(mean)'"
Cummulative ROC model I needs: .7595839500427246

. quietly sum roccumII

. display "Cummulative ROC model II strategic: `r(mean)'"
Cummulative ROC model II strategic: .7393428087234497

. quietly sum roccumIII

. display "Cummulative ROC model III baseline: `r(mean)'"
Cummulative ROC model III baseline: .701090395450592 */


 
 pause

 

* 2.2) CBPF needs vs strategic vs full  


set graphics off 
display "`vlist'"
 
drop fcast*
drop roc* 
 

set seed 586358

forvalues seed = 1(1)10 {
    display "Now doing iteration: `seed'/10"
    forvalues current_fold = 0(1)4 {
        display "k-fold is: `current_fold' (out of 5-folds)"

        display "Model I needs: "
        qui logit cbpfd mag1 ltotalaffectedemdata1000 totalsfi  conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country)
        predict fcastI`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model II strategic1 : " //we can take out agree here and rerun it which is easier as looping again over everything
		qui logit cbpfd ptassigned  p5colony ip_lnacorcount resid_emergoda agree  conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model IV full: "
        qui logit cbpfd mag1 ltotalaffectedemdata1000  conflict  totalsfi ptassigned  p5colony ip_lnacorcount resid_emergoda agree drought extmix flood storm if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastIII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
        eststo model`current_fold'`seed'

		
        roctab cbpfd fcastI`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucI`current_fold'`seed'=r(area)

        roctab cbpfd  fcastII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucII`current_fold'`seed'=r(area)

        roctab cbpfd  fcastIII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucIII`current_fold'`seed'=r(area)

        local vname fcastI`current_fold'`seed' fcastII`current_fold'`seed' fcastIII`current_fold'`seed'
        local vlist `vlist' cbpfd `vname'

    }
}

set graphics on 
display "`vlist'"

gen roccumI = 0
gen roccumII = 0
gen roccumIII = 0


forvalues i=0(1)4 {
     forvalues j=1(1)10 {
           replace roccumI = roccumI + rocaucI`i'`j'
           replace roccumII = roccumII + rocaucII`i'`j'
           replace roccumIII = roccumIII + rocaucIII`i'`j'
     }
}

replace roccumI = roccumI/50
replace roccumII = roccumII/50
replace roccumIII = roccumIII/50


estpost summarize rocauc*


quietly sum roccumI
display "Cummulative ROC model I needs: `r(mean)'"
quietly sum roccumII
display "Cummulative ROC model II strategic: `r(mean)'"
quietly sum roccumIII
display "Cummulative ROC model IV full: `r(mean)'"

/*
. display "Cummulative ROC model I needs: `r(mean)'"
Cummulative ROC model I needs: .7595839500427246

. quietly sum roccumII

. display "Cummulative ROC model II strategic: `r(mean)'"
Cummulative ROC model II strategic: .7393428087234497

. quietly sum roccumIII

. display "Cummulative ROC model IV full: `r(mean)'"
Cummulative ROC model IV full: .7853516936302185 */

 
 
 pause 
 
 
 
* 3) Other funds

* 3.1) Other funds needs vs strategic vs baseline 
 
 
drop fcast*
drop roc* 

  
set graphics off 
display "`vlist'"


set seed 586358

forvalues seed = 1(1)10 {
    display "Now doing iteration: `seed'/10"
    forvalues current_fold = 0(1)4 {
        display "k-fold is: `current_fold' (out of 5-folds)"

        display "Model I needs: "
        qui logit otherd mag1 ltotalaffectedemdata1000  totalsfi conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country)
        predict fcastI`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model II strategic: "  
		qui logit otherd ptassigned  p5colony ip_lnacorcount resid_emergoda agree conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model III baseline: "
        qui logit otherd  conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastIII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
        eststo model`current_fold'`seed'

		
        roctab otherd fcastI`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucI`current_fold'`seed'=r(area)
     
        
        roctab otherd  fcastII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucII`current_fold'`seed'=r(area)
    

        roctab otherd  fcastIII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucIII`current_fold'`seed'=r(area)
    

        local vname fcastI`current_fold'`seed' fcastII`current_fold'`seed' fcastIII`current_fold'`seed'
        local vlist `vlist' otherd `vname'

    }
}

set graphics on 
display "`vlist'"

gen roccumI = 0
gen roccumII = 0
gen roccumIII = 0


forvalues i=0(1)4 {
     forvalues j=1(1)10 {
           replace roccumI = roccumI + rocaucI`i'`j'
           replace roccumII = roccumII + rocaucII`i'`j'
           replace roccumIII = roccumIII + rocaucIII`i'`j'
         
     }
}

replace roccumI = roccumI/50
replace roccumII = roccumII/50
replace roccumIII = roccumIII/50

estpost summarize rocauc*

quietly sum roccumI
display "Cummulative ROC model I needs: `r(mean)'"
quietly sum roccumII
display "Cummulative ROC model II strategic: `r(mean)'"
quietly sum roccumIII
display "Cummulative ROC model III baseline: `r(mean)'"


/* 
. display "Cummulative ROC model I needs: `r(mean)'"
Cummulative ROC model I needs: .8459047675132751

. quietly sum roccumII

. display "Cummulative ROC model II strategic: `r(mean)'"
Cummulative ROC model II strategic: .8057661652565002

. quietly sum roccumIII

. display "Cummulative ROC model III baseline: `r(mean)'"
Cummulative ROC model III baseline: .7683497071266174

. 
*/
 
pause 

 
 
* 3.2) Other funds needs vs strategic vs full


set graphics off 
display "`vlist'"
 
drop fcast*
drop roc* 
 
  

set seed 586358

forvalues seed = 1(1)10 {
    display "Now doing iteration: `seed'/10"
    forvalues current_fold = 0(1)4 {
        display "k-fold is: `current_fold' (out of 5-folds)"

        display "Model I needs: "
        qui logit otherd mag1 ltotalaffectedemdata1000 totalsfi  conflict  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country)
        predict fcastI`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model II strategic1 : " //we can take out agree here and rerun it which is easier as looping again over everything
		qui logit otherd ptassigned  p5colony ip_lnacorcount resid_emergoda conflict agree  drought extmix flood storm  if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
		eststo model`current_fold'`seed'

        display "Model IV full: "
        qui logit otherd mag1 ltotalaffectedemdata1000  conflict  totalsfi ptassigned  p5colony ip_lnacorcount resid_emergoda agree drought extmix flood storm if samplecv==1 & fold`seed'!=`current_fold', nolog cl(country) 
        predict fcastIII`current_fold'`seed' if fold`seed' == `current_fold' & samplecv==1
        eststo model`current_fold'`seed'

		
        roctab otherd fcastI`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucI`current_fold'`seed'=r(area)
 
        
        roctab otherd  fcastII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucII`current_fold'`seed'=r(area)
      

        roctab otherd  fcastIII`current_fold'`seed' if fcastIII`current_fold'`seed'+fcastII`current_fold'`seed'+fcastI`current_fold'`seed' != .
        gen rocaucIII`current_fold'`seed'=r(area)
   

        local vname fcastI`current_fold'`seed' fcastII`current_fold'`seed' fcastIII`current_fold'`seed'
        local vlist `vlist' otherd `vname'

    }
}

set graphics on 
display "`vlist'"

gen roccumI = 0
gen roccumII = 0
gen roccumIII = 0


forvalues i=0(1)4 {
     forvalues j=1(1)10 {
           replace roccumI = roccumI + rocaucI`i'`j'
           replace roccumII = roccumII + rocaucII`i'`j'
           replace roccumIII = roccumIII + rocaucIII`i'`j'
  
     }
}

replace roccumI = roccumI/50
replace roccumII = roccumII/50
replace roccumIII = roccumIII/50


estpost summarize rocauc*

quietly sum roccumI
display "Cummulative ROC model I needs: `r(mean)'"
quietly sum roccumII
display "Cummulative ROC model II strategic: `r(mean)'"
quietly sum roccumIII
display "Cummulative ROC model IV full: `r(mean)'"
c
/*

. display "Cummulative ROC model I needs: `r(mean)'"
Cummulative ROC model I needs: .8459047675132751

. quietly sum roccumII

. display "Cummulative ROC model II strategic: `r(mean)'"
Cummulative ROC model II strategic: .8057661652565002

. quietly sum roccumIII

. display "Cummulative ROC model IV full: `r(mean)'"
Cummulative ROC model IV full: .8600089550018311 */







 
 
   
